package com.gang.controller;


import com.gang.conf.FiscoBcos;
import com.gang.pojo.CommonResult;
import io.swagger.models.auth.In;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.client.protocol.response.BcosBlock;
import org.fisco.bcos.sdk.client.protocol.response.BcosTransaction;
import org.fisco.bcos.sdk.client.protocol.response.BlockNumber;
import org.fisco.bcos.sdk.client.protocol.response.TotalTransactionCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/blockinfo")
@ResponseBody
public class BlockInfoController {

    @Autowired
    FiscoBcos fiscoBcos;

    @ResponseBody
    @RequestMapping("/getinfo")
    public CommonResult getBlocknum(){
        Client client = fiscoBcos.getBcosSDK().getClient(Integer.valueOf(1));
        BigInteger blockNumber = client.getBlockNumber().getBlockNumber();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("blocknum",blockNumber);
        map.put("node",4);
        String txSum = client.getTotalTransactionCount().getTotalTransactionCount().getTxSum();
        txSum=txSum.replaceAll("0x","");
        int i = Integer.parseInt(txSum, 16);
        map.put("tx",i);
        return new CommonResult(200,"info",map);
    }

    @RequestMapping("/getallblock/{currentPage}")
    @ResponseBody
    public List getAllBlock(@PathVariable("currentPage")int currentPage){
        Client client = fiscoBcos.getBcosSDK().getClient(Integer.valueOf(1));
        BlockNumber blockNumber = client.getBlockNumber();
        Integer i=Integer.valueOf(blockNumber.getResult().substring(2),16);
        List<BcosBlock> list=new ArrayList();
        for(int j = 0; j<=i; j++){
            BcosBlock blockByNumber = client.getBlockByNumber(BigInteger.valueOf(j), true);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date=new Date(Long.valueOf(blockByNumber.getResult().getTimestamp().substring(2),16));
            String format = sdf.format(date);
            blockByNumber.getResult().setTimestamp(format);
            list.add(blockByNumber);
        }
        Collections.reverse(list);
        if(currentPage*5>=i)
            return list.subList((currentPage-1)*5,i-1);
        else
            return list.subList((currentPage-1)*5,currentPage*5);
    }
    @RequestMapping("/getblockcount")
    public int getBlockCount(){
        Client client = fiscoBcos.getBcosSDK().getClient(Integer.valueOf(1));
        BlockNumber blockNumber = client.getBlockNumber();
        return Integer.valueOf(blockNumber.getResult().substring(2),16);
    }

    @RequestMapping("/gettxcount")
    public int getTxCount(){
        Client client = fiscoBcos.getBcosSDK().getClient(Integer.valueOf(1));
        TotalTransactionCount totalTransactionCount = client.getTotalTransactionCount();
        return Integer.valueOf(totalTransactionCount.getResult().getTxSum().substring(2),16);
    }

    @RequestMapping("/getalltx/{currentPage}")
    @ResponseBody
    public List getAllTx(@PathVariable("currentPage")int currentPage){
        Client client = fiscoBcos.getBcosSDK().getClient(Integer.valueOf(1));
        BlockNumber blockNumber = client.getBlockNumber();
        Integer i=Integer.valueOf(blockNumber.getResult().substring(2),16);
        List<BcosTransaction> list =new ArrayList<>();
        for(int j = 0; j<=i; j++){
            BcosBlock blockByNumber = client.getBlockByNumber(BigInteger.valueOf(j), true);
            for(int m=0;m<blockByNumber.getResult().getTransactions().size();m++) {
                BcosTransaction transactions = client.getTransactionByBlockNumberAndIndex(BigInteger.valueOf(j), BigInteger.valueOf(m));
                if(transactions.getResult().getTo().equals("0x0c708d5075d26dc0ed69a691f757b7162197bdda"))
                    transactions.getResult().setTo("did");
                if (transactions.getResult().getTo().equals("0x5293c2aff9d56f6c8c4c8d497a9a42d7dd47564b"))
                    transactions.getResult().setTo("验证");
                if (transactions.getResult().getTo().equals("0x2f3caed7fadddba8d4a7119734c46cff2138b377"))
                    transactions.getResult().setTo("NFT");
                if (transactions.getResult().getTo().equals("0x0000000000000000000000000000000000000000"))
                    transactions.getResult().setTo("其他");

                list.add(transactions);
            }
        }
        TotalTransactionCount totalTransactionCount = client.getTotalTransactionCount();
        Integer s=Integer.valueOf(totalTransactionCount.getResult().getTxSum().substring(2),16);
        Collections.reverse(list);
        if(currentPage*5>=s)
            return list.subList((currentPage-1)*5,s-1);
        else
            return list.subList((currentPage-1)*5,currentPage*5);
    }

}
